package com.gexiaobao.pigeon.ui.mall

import android.view.View
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.AddressListResponse
import com.gexiaobao.pigeon.app.model.bean.AfterSalesInfoDetailsResponse
import com.gexiaobao.pigeon.app.model.bean.AfterSalesListResponse
import com.gexiaobao.pigeon.app.model.bean.BuyCountResponse
import com.gexiaobao.pigeon.app.model.bean.CarCountResponse
import com.gexiaobao.pigeon.app.model.bean.CarouselListResponse
import com.gexiaobao.pigeon.app.model.bean.CartCouponListResponse
import com.gexiaobao.pigeon.app.model.bean.ChangeAddressResponse
import com.gexiaobao.pigeon.app.model.bean.ChangeCartSpecResponse
import com.gexiaobao.pigeon.app.model.bean.CheckGoodsResponse
import com.gexiaobao.pigeon.app.model.bean.CollectGoodsResponse
import com.gexiaobao.pigeon.app.model.bean.ConfirmAnOrderResponse
import com.gexiaobao.pigeon.app.model.bean.ConsumeResponse
import com.gexiaobao.pigeon.app.model.bean.DepositApplyResponse
import com.gexiaobao.pigeon.app.model.bean.EvaluateImageResponse
import com.gexiaobao.pigeon.app.model.bean.FlashSaleGoodsListResponse
import com.gexiaobao.pigeon.app.model.bean.FlashSaleResponse
import com.gexiaobao.pigeon.app.model.bean.FreezeMoneyResponse
import com.gexiaobao.pigeon.app.model.bean.GoodsDetailCouponListResponse
import com.gexiaobao.pigeon.app.model.bean.GoodsDetailResponse
import com.gexiaobao.pigeon.app.model.bean.GoodsListResponse
import com.gexiaobao.pigeon.app.model.bean.GoodsSkuResponse
import com.gexiaobao.pigeon.app.model.bean.GoodsTypeParentResponse
import com.gexiaobao.pigeon.app.model.bean.HotGoodsResponse
import com.gexiaobao.pigeon.app.model.bean.LogisticsProgressResponse
import com.gexiaobao.pigeon.app.model.bean.MallShopListResponse
import com.gexiaobao.pigeon.app.model.bean.MerchantStoresInfoResponse
import com.gexiaobao.pigeon.app.model.bean.MyShopOrderListResponse
import com.gexiaobao.pigeon.app.model.bean.PlatFormIdResponse
import com.gexiaobao.pigeon.app.model.bean.QueryBalanceResponse
import com.gexiaobao.pigeon.app.model.bean.QueryExpressResponse
import com.gexiaobao.pigeon.app.model.bean.ReturnProgressResponse
import com.gexiaobao.pigeon.app.model.bean.SaleTimesListResponse
import com.gexiaobao.pigeon.app.model.bean.ShopCarListResponse
import com.gexiaobao.pigeon.app.model.bean.ShopOrderListResponse
import com.gexiaobao.pigeon.app.model.bean.ShopOrderResponse
import com.gexiaobao.pigeon.app.model.bean.ShopServiceListResponse
import com.gexiaobao.pigeon.app.model.bean.StoreInfoResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.bean.ViewCommentsResponse
import com.gexiaobao.pigeon.app.model.param.AddOrReGoodsParam
import com.gexiaobao.pigeon.app.model.param.AddShoppingCarParam
import com.gexiaobao.pigeon.app.model.param.BuyNowParam
import com.gexiaobao.pigeon.app.model.param.CancelCollectParam
import com.gexiaobao.pigeon.app.model.param.CancelOrderParam
import com.gexiaobao.pigeon.app.model.param.CarCheckedAllItemParam
import com.gexiaobao.pigeon.app.model.param.CarCheckedItemParam
import com.gexiaobao.pigeon.app.model.param.CheckCouponParam
import com.gexiaobao.pigeon.app.model.param.CheckOrCancelShopParam
import com.gexiaobao.pigeon.app.model.param.CollectParam
import com.gexiaobao.pigeon.app.model.param.ConsumeParam
import com.gexiaobao.pigeon.app.model.param.DelayShopReceiptParam
import com.gexiaobao.pigeon.app.model.param.GenerateShopOrderParam
import com.gexiaobao.pigeon.app.model.param.ModifyAddressParam
import com.gexiaobao.pigeon.app.model.param.ModifyReceiveAddressParam
import com.gexiaobao.pigeon.app.model.param.PayParam
import com.gexiaobao.pigeon.app.model.param.ReSendSMSParam
import com.gexiaobao.pigeon.app.model.param.RefundReturnParam
import com.gexiaobao.pigeon.app.model.param.UploadExpressParam
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

class MallViewModel : BaseViewModel() {

    private var pageSize = 20
    private var pageNo: Int = 1

    val chargeDiscribe = StringObservableField()

    /** 充值按钮是否显示 */
    var chargeVisible = object : ObservableInt(chargeDiscribe) {
        override fun get(): Int {
            return if (chargeDiscribe.get().isEmpty()) {
                View.GONE
            } else {
                View.VISIBLE
            }
        }
    }

    /** 更改收货地址 */
    val changeAddressResult: MutableLiveData<ChangeAddressResponse> = MutableLiveData()
    fun changeAddress(body: ModifyReceiveAddressParam) {
        request({ apiService.changeAddress(body) }, {
            changeAddressResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取保证金信息 */
    val freezeMoneyResult: MutableLiveData<FreezeMoneyResponse> = MutableLiveData()
    fun getTotalFreezeMoney() {
        request({ apiService.getTotalFreezeMoney() }, {
            freezeMoneyResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 进店首页 */
    val storeInfoResult: MutableLiveData<MerchantStoresInfoResponse> = MutableLiveData()
    fun getAppShopIndex(shopId: Int) {
        request({ apiService.getAppShopIndexInfo(pageNo, pageSize, shopId) }, {
            storeInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 退货详情（进度） */
    val reViewsResult: MutableLiveData<ReturnProgressResponse> = MutableLiveData()
    fun getReviewStatus(id: Int) {
        request({ apiService.getReviewStatus(id) }, {
            reViewsResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 售后详情 */
    val afterInfoDetailResult: MutableLiveData<AfterSalesInfoDetailsResponse> = MutableLiveData()
    fun getAfterSaleInfo(id: Int) {
        request({ apiService.getAfterSaleInfo(id) }, {
            afterInfoDetailResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }


    /** 进店首页 */
    val storeBaseInfoResult: MutableLiveData<StoreInfoResponse> = MutableLiveData()
    fun getAppShopIndexInfo() {
        request({ apiService.getAppShopInfo() }, {
            storeBaseInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 上传退货图片 */
    val evaluateImageResult: MutableLiveData<EvaluateImageResponse> = MutableLiveData()
    fun uploadFile(imageFile: File) {
        val body: RequestBody = imageFile.asRequestBody("multipart/form-data".toMediaTypeOrNull()) //表单类型
        val builder = MultipartBody.Builder().setType(MultipartBody.FORM)//表单类型
        builder.addFormDataPart("upLoadFile", imageFile.name, body) //添加图片数据，body创建的请求体
        val requestBody = builder.build()

        request({ apiService.uploadFile(requestBody) }, {
            evaluateImageResult.value = it
        }, {
            if (it.message != null) {
                RxToast.showToast(it.message)
            }
        })
    }

    /** 确认支付 */
    val payResult = MutableLiveData<ResultState<DepositApplyResponse>>()
    fun commitPay(body: PayParam) {
        request(
            { apiService.commitPay(body) },
            payResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 上传物流单号 */
    val upLoadExpressResult: MutableLiveData<Any> = MutableLiveData()
    fun uploadExpress(body: UploadExpressParam) {
        request(
            { apiService.uploadExpress(body) }, {
                upLoadExpressResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 物流公司查询 */
    val queryExpressResult = MutableLiveData<ListDataUiState<QueryExpressResponse.ListBean>>()
    fun queryExpress() {
        request(
            { apiService.queryExpress() }, {
                val listDataUiState = ListDataUiState(
                    isSuccess = true, isEmpty = it.isEmpty(), listData = it.list
                )
                queryExpressResult.value = listDataUiState
            }, {
                val listDataUiState = ListDataUiState(isSuccess = false, errMessage = it.msg, listData = arrayListOf<QueryExpressResponse.ListBean>())
                queryExpressResult.value = listDataUiState
            }
        )
    }

    /** 申请退换货 */
    val refundResult = MutableLiveData<UiState>()
    fun addReturnReview(body: RefundReturnParam) {
        request(
            { apiService.addReturnReview(body) }, {
                val result = UiState(isSuccess = true)
                refundResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                refundResult.value = result
            }
        )
    }

    /** 添加收藏 */
    val collectResult = MutableLiveData<UiState>()
    fun addCollectGood(body: CollectParam) {
        request(
            { apiService.addCollectGood(body) }, {
                val result = UiState(isSuccess = true)
                collectResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                collectResult.value = result
            }
        )
    }

    /** 取消收藏 */
    val cancelCollectResult = MutableLiveData<UiState>()
    fun cancelCollectGood(body: CollectParam) {
        request(
            { apiService.cancelCollectGood(body) }, {
                val result = UiState(isSuccess = true)
                cancelCollectResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                cancelCollectResult.value = result
            }
        )
    }

    /** 删除收藏 */
    val delCollectResult = MutableLiveData<UiState>()
    fun deleteCollectGood(body: CancelCollectParam) {
        request(
            { apiService.deleteCollectGood(body) }, {
                val result = UiState(isSuccess = true)
                delCollectResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                delCollectResult.value = result
            }
        )
    }

    /** 申请退换货 */
    val modifyAddressResult = MutableLiveData<UiState>()
    fun editOrderReceiveAddress(body: ModifyAddressParam) {
        request(
            { apiService.editOrderReceiveAddress(body) }, {
                val result = UiState(isSuccess = true)
                modifyAddressResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                modifyAddressResult.value = result
            }
        )
    }

    /** 查询订单列表 */
    val goodsListResult: MutableLiveData<ListDataUiState<GoodsListResponse.ListBean>> = MutableLiveData()
    fun getAppGoodList(shopGoodTypeId: String, sortType: String, goodName: String) {
        request({ apiService.getAppGoodList(shopGoodTypeId, sortType, goodName) }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = true,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = it.isEmpty(),
                listData = it.list
            )
            goodsListResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = false,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<GoodsListResponse.ListBean>()
                )
            goodsListResult.value = listDataUiState
        })
    }

    /** 获取购物车列表 */
    val shopCarResult: MutableLiveData<ListDataUiState<ShopCarListResponse.ListBean>> = MutableLiveData()
    fun getShopCartList() {
        request({ apiService.getShopCartList() }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = true,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = it.isEmpty(),
                listData = it.list
            )
            shopCarResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = false,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<ShopCarListResponse.ListBean>()
                )
            shopCarResult.value = listDataUiState
        })
    }

    /** 获取我的订单列表 */
    val shopOrderListResult: MutableLiveData<ListDataUiState<ShopOrderListResponse.ListBean>> = MutableLiveData()
    fun getMyOrderList(isRefresh: Boolean, orderStatus: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getMyOrderList(orderStatus, pageSize, pageNo) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            shopOrderListResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<ShopOrderListResponse.ListBean>()
                )
            shopOrderListResult.value = listDataUiState
        })
    }

    /** 物流信息查询 */
    val LogisticsResult: MutableLiveData<LogisticsProgressResponse> = MutableLiveData()
    fun queryNumDetail(num: String) {
        request({ apiService.queryNumDetail(num) }, {
            LogisticsResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取收藏列表 */
    val collectGoodsResult: MutableLiveData<ListDataUiState<CollectGoodsResponse.ListBean>> = MutableLiveData()
    fun getCollectGoodList(isRefresh: Boolean) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getCollectGoodList(pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            collectGoodsResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = false,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<CollectGoodsResponse.ListBean>()
                )
            collectGoodsResult.value = listDataUiState
        })
    }

    /** 获取我的(店铺)订单列表 */
    val myShopOrderListResult: MutableLiveData<ListDataUiState<MyShopOrderListResponse.ListBean>> = MutableLiveData()
    fun getMyOrderShopList(isRefresh: Boolean, orderStatus: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getMyOrderShopList(orderStatus, pageSize, pageNo) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            myShopOrderListResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = false,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<MyShopOrderListResponse.ListBean>()
                )
            myShopOrderListResult.value = listDataUiState
        })
    }

    /** 获取我的(店铺)订单列表 */
    val afterSalesResult: MutableLiveData<ListDataUiState<AfterSalesListResponse.ListGoods>> = MutableLiveData()
    fun afterSales(isRefresh: Boolean) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.afterSales(pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            afterSalesResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<AfterSalesListResponse.ListGoods>()
                )
            afterSalesResult.value = listDataUiState
        })
    }

    /** 获取分类父列表  */
    var goodsTypeParentResult: MutableLiveData<GoodsTypeParentResponse> = MutableLiveData()
    fun getGoodTypeFatherList(pid: String) {
        request(
            { apiService.getGoodTypeFatherList(pid) }, {
                goodsTypeParentResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 获取商品详情  */
    var goodsDetailResult: MutableLiveData<GoodsDetailResponse> = MutableLiveData()
    fun getGoodDetail(id: String) {
        request(
            { apiService.getGoodDetail(id) }, {
                goodsDetailResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 获取商铺服务列表  */
    var shopServiceListResult: MutableLiveData<ShopServiceListResponse> = MutableLiveData()
    fun getShopServiceList() {
        request(
            { apiService.getShopServiceList(99999, 1) }, {
                shopServiceListResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }


    /** 获取商品sku列表 */
    var goodsSkuResult: MutableLiveData<GoodsSkuResponse> = MutableLiveData()
    fun getGoodSkuList(id: String) {
        request(
            { apiService.getGoodSkuList(id) }, {
                goodsSkuResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 添加到购物车  */
    var addShoppingCarResult: MutableLiveData<Any> = MutableLiveData()
    fun addToShopCart(body: AddShoppingCarParam) {
        request(
            { apiService.addToShopCart(body) }, {
                addShoppingCarResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 购物车+  */
    var addOrReGoodsResult: MutableLiveData<BuyCountResponse> = MutableLiveData()
    fun addCount(body: AddOrReGoodsParam) {
        request(
            { apiService.addCount(body) }, {
                addOrReGoodsResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 购物车-  */
    fun reduceCount(body: AddOrReGoodsParam) {
        request(
            { apiService.reduceCount(body) }, {
                addOrReGoodsResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 购物车勾选或取消勾选  */
    var checkedResult: MutableLiveData<CheckGoodsResponse> = MutableLiveData()
    fun checkOrCancelCheck(body: CarCheckedItemParam) {
        request(
            { apiService.checkOrCancelCheck(body) }, {
                checkedResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 全部勾选或者全部取消  */
    var allCheckedResult: MutableLiveData<Any> = MutableLiveData()
    fun fullCheckOrCancelCheck(body: CarCheckedAllItemParam) {
        request(
            { apiService.fullCheckOrCancelCheck(body) }, {
                allCheckedResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 全部勾选或者全部取消  */
    var changeCarSpecResult: MutableLiveData<ChangeCartSpecResponse> = MutableLiveData()
    fun changeCartSpec(body: AddShoppingCarParam) {
        request(
            { apiService.changeCartSpec(body) }, {
                changeCarSpecResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 购物车生成订单  */
    var shopOrderResult: MutableLiveData<ShopOrderResponse> = MutableLiveData()
    fun generateShopOrder(body: GenerateShopOrderParam) {
        request(
            { apiService.generateShopOrder(body) }, {
                shopOrderResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 获取订单详情  */
    var orderDetailResult: MutableLiveData<ConfirmAnOrderResponse> = MutableLiveData()
    fun getShopOrderList(id: String) {
        request(
            { apiService.getShopOrderList(id) }, {
                orderDetailResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 删除订单  */
    var deleteOrderResult: MutableLiveData<UiState> = MutableLiveData()
    fun userDeleteOrder(body: AddOrReGoodsParam) {
        request(
            { apiService.userDeleteOrder(body) }, {
                val result = UiState(isSuccess = true)
                deleteOrderResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                deleteOrderResult.value = result
            }
        )
    }

    /** 删除购物车  */
    var deleteCarResult: MutableLiveData<UiState> = MutableLiveData()
    fun deleteShopCart(body: AddOrReGoodsParam) {
        request(
            { apiService.deleteShopCart(body) }, {
                val result = UiState(isSuccess = true)
                deleteCarResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                deleteCarResult.value = result
            }
        )
    }

    /** 获取购物车当前数量  */
    var carCountResult: MutableLiveData<CarCountResponse> = MutableLiveData()
    fun getShopCartCnt() {
        request(
            { apiService.getShopCartCnt() }, {
                carCountResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    //作用域
    val mainScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
    fun getPersonalInfo() {
        CoroutineScope(Dispatchers.Default).launch {
            mainScope.launch {
                async {
                    request({ apiService.getShopCartCnt() }, {
                        carCountResult.value = it
                    })
                }
                async {
                    request(
                        { apiService.queryBalance() },
                        queryBalanceResult
                    )
                }
                async {
                    request(
                        { apiService.getHotSaleList(1, 999999) }, {
                            HotGoodsListResult.value = it
                        }, {
                            RxToast.showToast(it.msg)
                        }
                    )
                }
            }
        }
    }

    /** 立即购买  */
    var buyNowResult: MutableLiveData<ShopOrderResponse> = MutableLiveData()
    fun buyNow(body: BuyNowParam) {
        request(
            { apiService.buyNow(body) }, {
                buyNowResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 立即购买  */
    var consumeResult: MutableLiveData<ResultState<ConsumeResponse>> = MutableLiveData()
    fun consumeMallApply(body: ConsumeParam) {
        request(
            { apiService.consumeMallApply(body) },
            consumeResult
        )
    }

    /** 确认收货  */
    var confirmReceivedResult: MutableLiveData<UiState> = MutableLiveData()
    fun confirmShopReceipt(body: AddOrReGoodsParam) {
        request(
            { apiService.confirmShopReceipt(body) }, {
                val result = UiState(isSuccess = true)
                confirmReceivedResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                confirmReceivedResult.value = result
            }
        )
    }

    /** 店铺选择或者取消  */
    var checkShopResult: MutableLiveData<Any> = MutableLiveData()
    fun checkOrCancelShop(body: CheckOrCancelShopParam) {
        request(
            { apiService.checkOrCancelShop(body) }, {
                checkShopResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 重新发送验证码  */
    var reSendSMSResult: MutableLiveData<UiState> = MutableLiveData()
    fun reSendSMS(body: ReSendSMSParam) {
        request(
            { apiService.reSendSMS(body) }, {
                val result = UiState(isSuccess = true)
                reSendSMSResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                reSendSMSResult.value = result
            }
        )
    }

    /** 移除已失效商品  */
    var removeInvalidGoodResult: MutableLiveData<UiState> = MutableLiveData()
    fun removeInvalidGoods() {
        request(
            { apiService.removeInvalidGoods() }, {
                val result = UiState(isSuccess = true)
                removeInvalidGoodResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                removeInvalidGoodResult.value = result
            }
        )
    }

    /** 取消订单  */
    var cancelOrderResult: MutableLiveData<UiState> = MutableLiveData()
    fun cancelOrder(body: CancelOrderParam) {
        request(
            { apiService.cancelOrder(body) }, {
                val result = UiState(isSuccess = true)
                cancelOrderResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                cancelOrderResult.value = result
            }
        )
    }

    /** 获取用户设置地址  */
    val addressResult: MutableLiveData<ListDataUiState<AddressListResponse.ListBean>> = MutableLiveData()
    fun getAddressList() {
        request({ apiService.getShippingAddress() }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = true,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = it.isEmpty(),
                listData = it.list
            )
            addressResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = false,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<AddressListResponse.ListBean>()
                )
            addressResult.value = listDataUiState
        })
    }

    /** 获取评价列表  */
    val viewCommentsResult: MutableLiveData<ViewCommentsResponse> = MutableLiveData()
    fun getReviewsList(id: Int, evalType: Int) {
        request({ apiService.getReviewsList(id, 1, 3, evalType) }, {
            viewCommentsResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 购物获取可使用的优惠券列表  */
    val cartCouponResult: MutableLiveData<CartCouponListResponse> = MutableLiveData()
    fun getCartCouponList(shopId: Int) {
        request({ apiService.getCartCouponList(shopId) }, {
            cartCouponResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 立即购买获取可使用的优惠券列表  */
    val buyNowCouponSizeResult: MutableLiveData<GoodsDetailCouponListResponse> = MutableLiveData()
    fun getBuyNowCouponList(specId: Int, buyCount: Int) {
        request({ apiService.getBuyNowCouponList(specId, buyCount) }, {
            buyNowCouponSizeResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 查询余额 */
    val queryBalanceResult = MutableLiveData<ResultState<QueryBalanceResponse>>()
    fun queryBalance() {
        request(
            { apiService.queryBalance() },
            queryBalanceResult,
            false,
            appContext.getString(R.string.loading)
        )
    }

    /** 优惠券选中或非选中 */
    val checkCouponResult = MutableLiveData<Any>()
    fun checkCoupon(body: CheckCouponParam) {
        request(
            { apiService.checkCoupon(body) }, {
                checkCouponResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 请求轮播图 */
    val carouselListResult = MutableLiveData<CarouselListResponse>()
    fun getCarouselList() {
        request(
            { apiService.getCarouselList() }, {
                carouselListResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 获取限时抢购列表 */
    val saleListResult = MutableLiveData<FlashSaleResponse>()
    fun getSaleList() {
        request(
            { apiService.getSaleList(1, 999999) }, {
                saleListResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 获取场次列表 */
    val saleTimeListResult = MutableLiveData<SaleTimesListResponse>()
    fun getSaleTimeList() {
        request(
            { apiService.getSaleTimeList(1, 999999) }, {
                saleTimeListResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 获取热卖商品列表 */
    val HotGoodsListResult = MutableLiveData<HotGoodsResponse>()
    fun getHotSaleList() {
        request(
            { apiService.getHotSaleList(1, 999999) }, {
                HotGoodsListResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 延长收货 */
    val delayReceiptResult = MutableLiveData<Any>()
    fun delayShopReceipt(param: DelayShopReceiptParam) {
        request(
            { apiService.delayShopReceipt(param) }, {
                delayReceiptResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 再来一单 */
    val moreOneResult: MutableLiveData<UiState> = MutableLiveData()
    fun moreOne(param: DelayShopReceiptParam) {
        request(
            { apiService.moreOne(param) }, {
                val result = UiState(isSuccess = true)
                moreOneResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                moreOneResult.value = result
            }
        )
    }

    /** 获取平台id */
    val platformIDResult = MutableLiveData<PlatFormIdResponse>()
    fun getPlatFormId() {
        request(
            { apiService.getPlatFormId() }, {
                platformIDResult.value = it
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 获取抢购场次商品详情 */
    val saleListDetailResult: MutableLiveData<ListDataUiState<FlashSaleGoodsListResponse.ListBean>> = MutableLiveData()
    fun getSaleListBySaleTimeId(isRefresh: Boolean, id: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request(
            { apiService.getSaleListBySaleTimeId(id, pageNo, pageSize) }, {
                pageNo++
                val listDataUiState = ListDataUiState(
                    isSuccess = true,
                    isRefresh = isRefresh,
                    isEmpty = it.isEmpty(),
                    total = it.total,
                    hasMore = it.hasMore(),
                    isFirstEmpty = isRefresh && it.isEmpty(),
                    listData = it.list,
                    startTime = it.startTime,
                    countDown = it.countDown,
                    state = it.state
                )
                saleListDetailResult.value = listDataUiState
            }, {
                val listDataUiState = ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<FlashSaleGoodsListResponse.ListBean>()
                )
                saleListDetailResult.value = listDataUiState
                RxToast.showToast(it.msg)
            }
        )
    }

    /** 获取app店铺列表 */
    val shopListResult: MutableLiveData<ListDataUiState<MallShopListResponse.ListBean>> = MutableLiveData()
    fun getAppShopList(isRefresh: Boolean, id: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request(
            { apiService.getAppShopList(id, pageSize, pageNo) }, {
                pageNo++
                val listDataUiState = ListDataUiState(
                    isSuccess = true,
                    isRefresh = isRefresh,
                    isEmpty = it.isEmpty(),
                    total = it.total,
                    hasMore = it.hasMore(),
                    isFirstEmpty = isRefresh && it.isEmpty(),
                    listData = it.list,
                )
                shopListResult.value = listDataUiState
            }, {
                val listDataUiState = ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<MallShopListResponse.ListBean>()
                )
                shopListResult.value = listDataUiState
                RxToast.showToast(it.msg)
            }
        )
    }

}